package com.treasure.hunt.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import com.treasure.hunt.R
import kotlinx.android.synthetic.main.dialog_loading.*

class LoadingDialog2(context: Context) : Dialog(context, R.style.CustomDialog) {

    private val rotate: RotateAnimation = RotateAnimation(
        0F, 359F, Animation.RELATIVE_TO_SELF,
        0.5F, Animation.RELATIVE_TO_SELF, 0.5F
    ).apply {
        interpolator = LinearInterpolator()
        duration = 1500
        repeatCount = -1
        fillAfter = false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_loading)
        setCanceledOnTouchOutside(false)
    }

    override fun dismiss() {
        stopRotate()
        super.dismiss()
    }

    private fun startRotate() {
        image_dialog_loading.let {
            it.animation = rotate
            it.startAnimation(rotate)
        }
    }

    private fun stopRotate() {
        if (image_dialog_loading.animation != null) {
            image_dialog_loading.clearAnimation()
        }
    }

    fun showLoading(content: String? = null) {
        this.show()
        image_dialog_loading.setImageResource(R.mipmap.ic_dialog_loading)
        text_dialog_loading.text = "加载中..."
        startRotate()
    }
}